package net.xju.controller;


import net.xju.common.framework.simplemvc.RequestMapping;
import net.xju.common.utils.DateUtils;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.PageInfo;
import net.xju.common.utils.Result;
import net.xju.entity.Goods;
import net.xju.entity.User;
import net.xju.entity.Vo.GoodsVo;
import net.xju.service.GoodsService;
import net.xju.service.impl.GoodsServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

@RequestMapping("/api/goods")
public class GoodsController {

    GoodsService goodsService = new GoodsServiceImpl();

//    //    新增数据
    @RequestMapping("/insert.do")
    public  void insert(HttpServletRequest req, HttpServletResponse resp) {

        Goods goods = new Goods();
        try {
            req.setCharacterEncoding("utf-8");
        } catch (java.io.UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }

        goods.setName(req.getParameter("name"));
        goods.setPrice(Integer.parseInt(req.getParameter("price")));
        goods.setPubdate(DateUtils.parseStr2Date(req.getParameter("pubdate")));
        goods.setTypeId(Integer.parseInt(req.getParameter("typeId")));
        goods.setIntro(req.getParameter("intro"));
        String picture = req.getParameter("picture");
        if (picture.isEmpty()){
            picture = null;
        }
        goods.setPicture(picture);
        goods.setFlag(Integer.parseInt(req.getParameter("flag")));
        goods.setCreateTime(LocalDateTime.now());
        if(goodsService.insert(goods)){
            JSONUtil.printByJSON(resp, Result.success(goods, "添加成功"));
        }else {
            JSONUtil.printByJSON(resp, Result.fail("添加失败"));
        }
    }
    //    根据id删除
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));
        System.out.println(id);
        if (goodsService.delete(id)){
            JSONUtil.printByJSON(resp, Result.success("删除成功"));
        }else {
            JSONUtil.printByJSON(resp, Result.fail("删除失败"));
        }

    }
//    //    修改数据
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) {
        Goods goods = new Goods();
        try {
            req.setCharacterEncoding("utf-8");
        } catch (java.io.UnsupportedEncodingException e) {
            JSONUtil.printByJSON(resp, Result.fail("字符编码设置失败"));
            return;
        }
        goods.setName(req.getParameter("name"));
        goods.setId(Integer.parseInt(req.getParameter("id")));
        if (!req.getParameter("price").trim().isEmpty()){
            goods.setPrice(Integer.parseInt(req.getParameter("price")));
        }
        goods.setPubdate(DateUtils.parseStr2Date(req.getParameter("pubdate")));
        if (!req.getParameter("typeId").trim().isEmpty()){
            goods.setTypeId(Integer.parseInt(req.getParameter("typeId")));
        }
        goods.setIntro(req.getParameter("intro"));
        goods.setPicture(req.getParameter("picture"));
        if (!req.getParameter("flag").trim().isEmpty()){
            goods.setFlag(Integer.parseInt(req.getParameter("flag")));
        }
        if(goodsService.update(goods)){
            JSONUtil.printByJSON(resp, Result.success(goods, "修改成功"));
        }else {
            JSONUtil.printByJSON(resp, Result.fail("修改失败"));
        }
    }

    //    分页查找
    @RequestMapping("/pageList.do")
    public void pagelist(HttpServletRequest req, HttpServletResponse resp) {

        //获取分页参数pageNum和pageSize
        String pageNumStr = req.getParameter("pageNum");
        int pageNum = (pageNumStr == null || pageNumStr.isEmpty())? 1 : Integer.parseInt(pageNumStr);
        String pageSizeStr = req.getParameter("pageSize");
        int pageSize = (pageSizeStr == null || pageSizeStr.isEmpty())? 10 : Integer.parseInt(pageSizeStr);

        //获取条件查询参数
        Goods goods = new Goods();
        goods.setName(req.getParameter("name"));
        String pubdateStr = req.getParameter("pubdate");
        if (pubdateStr != null && !pubdateStr.isEmpty()) {
            goods.setPubdate(DateUtils.parseStr2Date(pubdateStr));
        }
        if (req.getParameter("typeId")!= null&& !req.getParameter("typeId").isEmpty()){
            goods.setTypeId(Integer.parseInt(req.getParameter("typeId")));
        }
        if (req.getParameter("flag")!= null && !req.getParameter("flag").isEmpty()){
            goods.setFlag(Integer.parseInt(req.getParameter("flag")));
        }

        PageInfo<GoodsVo> pageInfo = goodsService.pageList(goods, pageNum, pageSize);
        if (pageInfo == null){
            JSONUtil.printByJSON(resp, Result.fail("没有数据"));
        }else{
            JSONUtil.printByJSON(resp, Result.success(pageInfo, "查询成功"));
        }
    }

}
